package flexo.input.entity;

import flexo.input.Event;
import flexo.math.Vector;
import flexo.scene.Node;

public class FPSLook extends Entity
{
	protected float hRotation,vPos;
		
	public static final float RADIUS = 1.1f;
	protected static final float VLIMIT = RADIUS;
	
	// Vertical movement is X of Horizontal movement
	// This is used to lower the sensitivity in vertical movement
	protected static final float VHPROPORTION = 0.80f;
	
	public static final String LOOK_UP = "FPS_LOOK_UP";
	public static final String LOOK_DOWN = "FPS_LOOK_DOWN";
	public static final String LOOK_LEFT = "FPS_LOOK_LEFT";
	public static final String LOOK_RIGHT = "FPS_LOOK_RIGHT";
	
	public FPSLook()
	{
		setSensitivity(0.25f);
		hRotation = (float)-Math.PI/2;
		vPos = 0;
	}
	
	@Override
	public boolean attachTo(Node n)
	{
		boolean result = super.attachTo(n);
		if (result)
		{
			updatePosition();
		}
		return result;
	}
	
	protected void updatePosition()
	{
		if (exclusiveNode != null)
		{
			exclusiveNode.setPosition(new Vector( RADIUS * Math.cos(hRotation),
					vPos,
					RADIUS * Math.sin(hRotation)
					));
		}
	}

	@Override
	protected void update(Event e, float step)
	{
		if (e.check(LOOK_UP))
		{
			vPos += step * VHPROPORTION;
			if (vPos > VLIMIT)
			{
				vPos = VLIMIT;
			}
		}
		else if (e.check(LOOK_DOWN))
		{	
			vPos -= step * VHPROPORTION;
			if (vPos < -VLIMIT)
			{
				vPos = -VLIMIT;
			}
		}
		else if (e.check(LOOK_LEFT))
		{			
			hRotation -= step;
		}
		else if (e.check(LOOK_RIGHT))
		{			
			hRotation += step;
		}
		updatePosition();
	}
}

